app.controller("logQueryCtrl", function($scope, logQueryService, $log) {
    $log.info('LogQueryController initialize');

    // 分页查询所用全局变量
    var startTime = (new Date()).Format("yyyy-MM-dd");
    var endTime = (new Date()).Format("yyyy-MM-dd");
    var serverName = '';
    var type = '';
    var content = '';
    /**
     * 页面初始化
     */
    $scope.init = function () {
        // 初始化查询条件
        $scope.initParam();

        // 初始化时间控件
        $scope.initDateBox();

        // 展示监控状态
        $scope.findLogs(1, 25);
    };

    /**
     * 初始化查询条件
     */
    $scope.initParam = function () {
        $scope.startTime = (new Date()).Format("yyyy-MM-dd");
        $scope.endTime = (new Date()).Format("yyyy-MM-dd");
        $scope.serverName = '';
        $scope.type = '';
        $scope.content = '';
    };

    /**
     * 初始化日期控件
     */
    $scope.initDateBox = function(){
        $('#startTime').datetimepicker({
            language:  'zh-CN',
            todayBtn:  true,
            autoclose: true,
            format: 'yyyy-mm-dd',
            minView: 'month',
            endDate: $scope.startTime
        }).on("changeDate",function(){
            $("#endTime").datetimepicker("setStartDate", $("#startTime").val());
        });

        $('#endTime').datetimepicker({
            language:  'zh-CN',
            todayBtn:  true,
            autoclose: true,
            format: 'yyyy-mm-dd',
            minView: 'month',
            startDate: $scope.endTime
        }).on("changeDate", function () {
            $("#startTime").datetimepicker("setEndDate", $("#endTime").val());
        });
    };

    /**
     * 分页查询通用方法
     */
    $scope.findByCondition = function () {
        var params = {
            action: 'find',
            serverName: serverName,
            type: type,
            content: content,
            startTime: startTime,
            endTime: endTime,
            currentPage: $scope.selPage,
            pageSize: $scope.selectPageSize
        };
        // 查询
        $scope.find(params);
    };

    /**
     * 查询系统日志
     * @param currentPage
     * @param pageSize
     */
    $scope.findLogs = function (currentPage, pageSize) {
        //分页查询条件赋值
        serverName = $scope.serverName;
        type = $scope.type;
        content = $scope.content;
        startTime = $scope.startTime;
        endTime = $scope.endTime;

        var params = {
            action:"find",
            serverName: $scope.serverName,
            type: $scope.type,
            content: $scope.content,
            startTime: $scope.startTime,
            endTime: $scope.endTime,
            currentPage:$scope.selPage,
            pageSize:$scope.selectPageSize
        };
        if(!isNullOrEmpty(currentPage)){
            params["currentPage"] = currentPage;
        }
        if(!isNullOrEmpty(pageSize)){
            params["pageSize"] = pageSize;
        }
        // 查询
        $scope.find(params);

    };

    /**
     * 统一查询方法
     * @param params
     */
    $scope.find = function (params) {
        $.fn.jqLoading({height: 80, width: 240, text: "正在加载中，请耐心等待...."});
        logQueryService.find(params, function (data) {
            $.fn.jqLoading("destroy");
            if (data.status === 'SUCCESS') {
                $log.info("show logs success");
                $scope.serverLogs = data.data.logs;
                pageOperation($scope, data.data.pageInfo);
            } else {
                message_alert('提示', data.errorReason,null);
            }
        }, function () {
            $.fn.jqLoading("destroy");
        });
    };


});